<?php
/**
* File that contains Application_Form_Admin_editBusstop class.
* @package busmap
*/

/**
* Application_Form_Admin_editBusstop class generates a form used to edit busstops.
* Form has a text field for the busstop name.
* Two text fields for x- and y-coordinate of the busstop (although these values are more easily edited directly from the map).
* There is a subform that shows all the buslines that go through that busstop. These values aren't editable from this form.
* @package busmap
*/
class Application_Form_Admin_editBusstop extends Zend_Form
{
	/**
	* Initialize function generates all the elements described in the class description.
	*/
    public function init()
    {
        $this->setMethod('post');

        $this->addElement(
            'hidden', 'id', array(
                'required' => true
            ));
 
        $this->addElement(
            'text', 'name', array(
                'required' => true,
                'filters'    => array('StringTrim'),
				'class' => 'bold-text'
            ));
		$this->name->addErrorMessage('Bussipeatuse nimi ei saa olla tühi');
		$this->name->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));

		$this->addElement(
            'text', 'xCoordinate', array(
                'required' => true,
				'validators' => array('Digits'),
				'class' => 'small-data'
            ));
		$this->xCoordinate->addErrorMessage('Bussipeatuse x-kordinaat peab olema number');
		$this->xCoordinate->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell'));

		$this->addElement(
            'text', 'yCoordinate', array(
                'required' => true,
				'validators' => array('Digits'),
				'class' => 'small-data'
            ));
		$this->yCoordinate->addErrorMessage('Bussipeatuse y-kordinaat peab olema number');
		$this->yCoordinate->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell'));


		$buslinesForm = new Zend_Form_SubForm('buslinesForm');
		$buslinesForm->removeDecorator('fieldset');
		$buslinesForm->removeDecorator('DtDdWrapper');
		$buslinesForm->setName('buslinesForm');
		$this->addSubForm($buslinesForm, 'buslinesForm');
		$this->buslinesForm->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell busstop-listing'));
		
		 $this->addElement(
            'submit', 'change', array(
                'required' => false,
				'label' => 'Muuda',
				'class' => 'submit-data'
            ));
		$this->change->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));

		 $this->addElement(
            'submit', 'delete', array(
                'required' => false,
				'label' => 'Kustuta',
				'class' => 'delete-data'
            ));
		$this->delete->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));
 
		$this->clearElementLabels();
		
    }

	/**
	* This function populates the buslinesForm subform with buslines sent to it.
	* For each busline a corresponding background color is being stylized. 
	*/
	public function populateWithBusLines($buslines){
		foreach($buslines as $buslineData){
			list($busline, $color) = $buslineData;
			$this->buslinesForm->addElement(
            'text', 'busline:'.$busline->id, array(
                'required' => false,
				'ignore' => true,
				'readonly' => true,
				'style' => 'border:0px;background-color:transparent;color:black;',
				'value' => $busline->name
            ));
			$this->buslinesForm->{'busline:'.$busline->id}->addDecorator('HtmlTag', array(
					'tag'=>'div', 'style'=>'background-color:rgb('.$color->redValue.','.$color->greenValue.','.$color->blueValue.')', 
					'class'=>'data-listing data-listing-cell-button'
					)
			);
			$this->buslinesForm->{'busline:'.$busline->id}->removeDecorator('label')->removeDecorator('DtDdWrapper')->removeDecorator('Errors');
		}
	}

	/**
	* This function is called when a delete request is received.
	* It removes the delete button and adds two new buttons. One of which is a delete confirmation button that upon sending deletes the corresponding entry.
	*/
	public function askDeleteConfirmation(){
		$this->removeElement('delete');

		$this->addElement('submit', 'back', array(
			 'ignore'   => true,
			 'label'	=>'Ära kustuta',
			 'class'	=>'submit-data'
		));
		$this->back->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell-button'));
		
		$this->addElement('submit', 'delete_confirm', array(
			 'ignore'   => true,
			 'label'	=>'Kustuta! [Kinnitus]',
			 'class'	=>'delete-data'
		));
		$this->delete_confirm->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell-button delete-data'));

		$this->clearElementLabels();

	}

	/**
	* This function removes all label, DtDd and Error decorators.
	* Error messages are being shown elsewhere in view.
	*/
	public function clearElementLabels(){
		$elements = $this->getElements();
		foreach($elements as $element){
			$element->removeDecorator('label')->removeDecorator('DtDdWrapper')->removeDecorator('Errors');
		}
	}

}